package com.li.jiang.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.li.jiang.Exception.DataException;
import com.li.jiang.obj.pojo.Company;
import com.li.jiang.mapper.CompanyMapper;
import com.li.jiang.obj.pojo.CompanyVO;
import com.li.jiang.service.ICompanyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 企业信息表 服务实现类
 * </p>
 *
 * @author 李江
 * @since 2024-04-12
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements ICompanyService {
    @Override
    public CompanyVO getCompanyByUid(Long uid) {
        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<Company>().eq(Company::getUid,uid);
        Company company = getOne(wrapper);

        return BeanUtil.copyProperties(company,CompanyVO.class);
    }

    @Override
    public Company getCompanyByName(String companyName) {
        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<Company>().eq(Company::getName,companyName);
        return getOne(wrapper);
    }

    @Override
    public Company UpdateCompanyByCid(Company company) {
        Company baseCompany = this.getById(company.getId());
        if (baseCompany == null){
            throw new DataException("公司信息错误或不存在!!");
        }
        LambdaUpdateWrapper<Company> wrapper = new LambdaUpdateWrapper<Company>().eq(Company::getId,company.getId());
        this.update(company,wrapper);
        return this.getById(company.getId());
    }
}
